package com.atguigu.flink.chapter07;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.util.KKutil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.TimestampAssignerSupplier;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink10_Watermark {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        env
                .socketTextStream("hadoop162", 9999)
                .map(line ->{
                    String[] strings = line.split(",");
                    return new WaterSensor(
                            strings[0],
                            Long.valueOf(strings[1]),
                            Integer.valueOf(strings[2]));
                })

                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                //允许无序的时候，迟到时间2s
                        .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(1))
                               //返回值long值当作一个一个毫秒值
                                .withTimestampAssigner((ele, ts) -> ele.getTs())

                        //避免数据倾斜导致的水印不更新  某个并行度任务的水印超过5s不更新, 则使用其他的并行度的水印强制更新
                        .withIdleness(Duration.ofSeconds(1))

                )



                .keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))

                //允许迟到时间  设置允许元素延迟的时间。 超过指定时间到达水印之后的元素将被丢弃。 默认情况下，允许的延迟为0L 。
                //设置允许的延迟仅对    事件时间窗口   有效
               //.allowedLateness(Time.seconds(10))
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String value, Context ctx, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {

                        List<WaterSensor> list = KKutil.toList(elements);
                        out.collect(value+"  "+ elements);

                    }
                })

                .print();


        env.execute();
    }

  /*
  * keyby 之前使用窗口：
  *         所有的元素都会进入同一窗口
  *
  * */

}
